home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / strategy / xpat2-1.000 / xpat2-1 / xpat2-1.04 / src / Xlib-windows.c < prev    next >
C/C++ Source or Header  |  1994-09-28  |  7KB  |  205 lines

  1. /*****************************************************************************/
  2. /*                                         */
  3. /*                                         */
  4. /*    X patience version 2 -- module Xlib-windows.c                 */
  5. /*                                         */
  6. /*    windows creation for the Xlib interface                     */
  7. /*    written by Heiko Eissfeldt and Michael Bischoff                 */
  8. /*    based on the windows.c module from Spider by David Lemke         */
  9. /*    see COPYRIGHT.xpat2 for Copyright details                 */
  10. /*                                         */
  11. /*                                         */
  12. /*****************************************************************************/
  13. #ifdef useXlib
  14.  
  15. #include "X-pat.h"
  16.  
  17. #include "pat.bm"
  18. #include "gray1.bm"
  19.  
  20. void init_fonts(const char *statusfont, const char *buttonfont)
  21. {   static const char *buttonfonttable[] = {
  22.     NULL,
  23.     "-misc-fixed-bold-r-normal--13-120-75-75-c-70-iso8859-1",
  24.     "-misc-fixed-bold-r-normal--15-140-75-75-c-90-iso8859-1",
  25.     "-misc-fixed-medium-r-normal--20-200-75-75-c-100-iso8859-1",
  26.     "-misc-fixed-bold*",
  27.     "fixed"
  28.     };
  29.     int i;
  30.     XGCValues gcv;
  31.     long gcflags;
  32.  
  33.     gcv.foreground = BlackPixel(dpy, screen);
  34.     gcv.background = WhitePixel(dpy, screen);
  35.     gcv.graphics_exposures = False;
  36.     gcflags = GCForeground | GCBackground | GCGraphicsExposures;
  37.     
  38.     buttonfonttable[0] = buttonfont;
  39.     
  40.     /* add on to blackgc */
  41.     if ((graphic.message_font = XLoadQueryFont(dpy, statusfont)) == NULL) {
  42.     (void) fprintf(stderr,"can't get font %s\n", statusfont);
  43.     exit(0);
  44.     }
  45.     graphic.message_height = graphic.message_font->ascent + graphic.message_font->descent + 2 * TABLE_BW;
  46.     gcv.font = graphic.message_font->fid;
  47.     gcflags |= GCFont;
  48.     
  49.     graphic.textgc = XCreateGC(dpy, RootWindow(dpy, screen), gcflags, &gcv);
  50.  
  51.     for (i = 0; i < sizeof(buttonfonttable) / sizeof(buttonfonttable[0]); ++i) {
  52.     if (!buttonfonttable[i])
  53.         continue;    /* no user font */
  54.     if ((button.font = XLoadQueryFont(dpy, buttonfonttable[i])))
  55.         break;
  56.     fprintf(stderr,"warning: can't get font %s\n", buttonfonttable[i]);
  57.     }
  58.     if (!button.font) {
  59.     fprintf(stderr, "unable to get button font. terminating\n");
  60.     exit(EXIT_FAILURE);
  61.     }
  62.     
  63.     button.fontheight = button.font->ascent + button.font->descent;
  64.     
  65.     gcv.font = button.font->fid;
  66.     gcflags |= GCFont;
  67.     button.gc = XCreateGC(dpy, RootWindow(dpy, screen), gcflags, &gcv);
  68. }
  69.  
  70.  
  71. void init_windows(int ac, char **av, const char *geom,
  72.     int xgap, int ygap, int bradx, int brady, int bx, int by, int autolayout, const char *bkcolor)
  73. {
  74.     unsigned long borderpixel = blackpixel;
  75.     XSetWindowAttributes winattr;
  76.     long winmask;
  77.     XSizeHints xsh;
  78.     int x, y;
  79.     Pixmap icon_map;
  80.  
  81.     graphic.xgap = (XSize_t)xgap;
  82.     graphic.ygap = (XSize_t)ygap;
  83.     graphic.autolayout = autolayout;
  84.  
  85.     button.bx = (XSize_t)bx;
  86.     button.by = (XSize_t)by;
  87.     if (button.fontheight + 2 * button.by < 2 * brady)
  88.     brady = button.fontheight/2 + button.by;
  89.     B_ROUND_W = bradx;
  90.     B_ROUND_H = brady;
  91.  
  92.     /* compute minimum size needed */
  93.     generic_minwindow((XSize_t *)&xsh.min_width, (XSize_t *)&xsh.min_height);
  94.     graphic.width = xsh.min_width;
  95.     graphic.height = xsh.min_height;
  96.     graphic.min_width = graphic.width;
  97.     graphic.min_height = graphic.height;
  98.  
  99.     xsh.flags = PPosition | PSize | PMinSize;
  100.     x = y = 0;
  101.  
  102.     if (geom) {
  103.     int flags = XParseGeometry(geom, &x, &y, &graphic.width, &graphic.height);
  104.         
  105.     /* don't let it start too short */
  106.     if (flags & HeightValue && graphic.height < xsh.min_height)
  107.         graphic.height = xsh.min_height;
  108.     /* don't let it start too narrow */
  109.     if (flags & WidthValue && graphic.width < xsh.min_width)
  110.         graphic.width = xsh.min_width;
  111.  
  112.     if (flags & (WidthValue | HeightValue))
  113.         xsh.flags |= USSize;
  114.     if (flags & (XValue | YValue))
  115.         xsh.flags |= USPosition;
  116.  
  117.     if (flags & XValue && flags & XNegative)
  118.         x = DisplayWidth(dpy, screen) - (graphic.width + x);
  119.     if (flags & YValue && flags & YNegative)
  120.         y = DisplayHeight(dpy, screen) - (graphic.height + y);
  121.     }
  122.  
  123.     /* with internal cards, we're fast enough to forget the backing store */
  124.     /* (we already use a lot of memory to store the cards) */
  125.     /* in the case NO_MEM, we don't have the images ready, but using */
  126.     /* the backing store in this case would be a contradiction */
  127.     {
  128.     winattr.backing_store = card.cardset ? WhenMapped : NotUseful;
  129.     winattr.border_pixel = blackpixel;
  130.     winattr.bit_gravity = NorthWestGravity;
  131.     winattr.event_mask = KeyPressMask | ExposureMask | ButtonPressMask | 
  132.         ButtonReleaseMask | StructureNotifyMask | Button3MotionMask;
  133.     winmask = CWBorderPixel | CWEventMask | CWBackingStore | CWBitGravity;
  134.     
  135.     if (graphic.is_color)    {
  136.         XColor color;
  137.         Colormap cmap;
  138.         cmap = DefaultColormap(dpy, screen);
  139.         color.flags = DoRed | DoGreen | DoBlue;
  140.         XAllocNamedColor(dpy, cmap, bkcolor, &color, &color);
  141.         winattr.background_pixel = color.pixel;
  142.         winmask |= CWBackPixel;
  143.     } else    {
  144.         winattr.background_pixmap = XCreateBitmapFromData(dpy,
  145.           RootWindow(dpy, screen), gray1_bits, gray1_width, gray1_height);
  146.         winmask |= CWBackPixmap;
  147.     }
  148.     table = XCreateWindow(dpy, RootWindow(dpy, screen), 
  149.         x, y, graphic.width, graphic.height, TABLE_BW,
  150.         CopyFromParent, CopyFromParent, CopyFromParent, winmask, &winattr);
  151.     }
  152.  
  153.     xsh.x = x;
  154.     xsh.y = y;
  155.  
  156.     xsh.width = graphic.width;
  157.     xsh.height = graphic.height;
  158.  
  159.     icon_map = XCreateBitmapFromData(dpy, RootWindow(dpy, screen),
  160.     pat_bits, pat_width, pat_height);
  161.  
  162.     XSetStandardProperties(dpy, table, "Patience", "Patience", icon_map,
  163.     av, ac, &xsh);
  164.  
  165.     {   XWMHints xwmh;
  166.  
  167.     xwmh.flags = InputHint | IconPixmapHint;
  168.     xwmh.input = True;
  169.     xwmh.icon_pixmap = icon_map;
  170.     XSetWMHints(dpy, table, &xwmh);
  171.     }
  172.     XStoreName(dpy, table, rules.longname ? rules.longname : rules.shortname);
  173.     XSetIconName(dpy, table, rules.shortname);
  174.  
  175.     /* calculate size and position of confirm window */
  176.     {   int w;
  177.     long dummy;
  178.     x = graphic.xgap;
  179.     y = graphic.ygap + 3 * button.by + 2 * button.fontheight;
  180.     (void)arrange_buttons(confirmbuttons, &x, &y, 32767, &dummy);
  181.     w = XTextWidth(button.font, "Please confirm<space>", 21);
  182.     if (x < w)
  183.         x = w;
  184.     confirm.w = x;
  185.     confirm.h = y;
  186.     confirm.x = (graphic.width - x) / 2;
  187.     confirm.y = (graphic.height - y) / 2;
  188.     }
  189.     {
  190.     confirm.win = XCreateSimpleWindow(dpy, table, confirm.x, confirm.y,
  191.          confirm.w, confirm.h, TABLE_BW, borderpixel, whitepixel);
  192.     winattr.event_mask = ExposureMask | ButtonPressMask;
  193.     winmask = CWEventMask;
  194.     XChangeWindowAttributes(dpy, confirm.win, winmask, &winattr);
  195.  
  196.     finished_win = XCreateSimpleWindow(dpy, table, 0, 0,
  197.          FINISHED_W, FINISHED_H, TABLE_BW, borderpixel, whitepixel);
  198.     winattr.event_mask = ExposureMask;  /* shall receive exposure event */
  199.     winmask = CWEventMask;
  200.     XChangeWindowAttributes(dpy, finished_win, winmask, &winattr);
  201.     }
  202.     XMapWindow(dpy, table);
  203. }
  204. #endif
  205.